package com.hxs.learn.service.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hxs.learn.constants.core.sql.SqlColConts;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.hxs.learn.common.entity.TreeJson;
import com.hxs.learn.common.page.Page;
import com.hxs.learn.common.page.PageHelper;
import com.hxs.learn.common.page.PageWrapper;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.entity.core.SysUserRole;
import com.hxs.learn.mapper.core.SysUserRoleMapper;

/**
 * @author huxinsheng
 * @data 2018-04-24
 */
@Service
public class SysUserRoleService {
    private Logger                  logger = LoggerFactory.getLogger(SysUserRoleService.class);
    private final SysUserRoleMapper userRoleMapper;

    @Autowired
    public SysUserRoleService(SysUserRoleMapper userRoleMapper) {
        this.userRoleMapper = userRoleMapper;
    }

    /**
     * 查询所有
     *
     * @return List<SysUserRole>
     */
    public List<SysUserRole> findAllSysUserRole() {
        return userRoleMapper.selectList(new EntityWrapper<>(new SysUserRole()));
    }

    /**
     * 按主键查询
     *
     * @param id 主键id
     * @return SysUserRole
     */
    public SysUserRole findById(Integer id) {
        return userRoleMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param sysUserRole 分页条件对象
     * @return PageWrapper<SysUserRole>
     */
    public PageWrapper<SysUserRole> findSysUserRolePageData(Page page, SysUserRole sysUserRole) {
        return PageHelper.getPageData(userRoleMapper, page, sysUserRole);
    }

    /**
     * 按主键删除
     *
     * @param id 主键id
     */
    @Transactional(rollbackFor = Exception.class)
    public Result removeById(Integer id) {
        userRoleMapper.deleteById(id);

        return Result.ok();
    }

    /**
     * 保存
     *
     * @param userInfo    登录用户信息
     * @param sysUserRole 用户角色信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Result save(LoginInfo userInfo, SysUserRole sysUserRole) {
        String   roleId  = sysUserRole.getRoleId();
        String   userId  = sysUserRole.getUserId();
        String[] roleIds = StringUtils.split(roleId, ",");

        if (ArrayUtils.isEmpty(roleIds)) {
            return Result.error("该用户未添加任何角色");
        }

        EntityWrapper<SysUserRole> wrapper = new EntityWrapper<>();

        wrapper.eq(SqlColConts.USER_ID, userId);

        // 先删除之前关联的菜单数据
        userRoleMapper.delete(wrapper);

        List<SysUserRole> pojos = new ArrayList<>(roleIds.length);

        for (String id : roleIds) {
            SysUserRole pojo = new SysUserRole();

            pojo.setRoleId(id);
            pojo.setUserId(userId);
            pojo.setCreateTime(new Date());
            pojo.setCreator(userInfo.getLoginName());
            pojos.add(pojo);
        }

        userRoleMapper.insertAll(pojos);

        return Result.ok();
    }

    public List<TreeJson<Object>> getUserRoleTreeJson(String userId) {
        return userRoleMapper.selectByUserId(userId);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
